package com.hero.part.boot.controller;

import org.pac4j.core.config.Config;
import org.pac4j.core.context.J2EContext;
import org.pac4j.core.engine.DefaultLogoutLogic;
import org.pac4j.core.engine.LogoutLogic;
import org.pac4j.core.http.J2ENopHttpActionAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yuez
 * @date 2018/3/5
 */
@Controller
public class LogoutController {
	private LogoutLogic<Object, J2EContext> logoutLogic = new DefaultLogoutLogic<>();

	@Value("${pac4j.logout.defaultUrl:#{null}}")
	private String defaultUrl;

	@Value("${pac4j.logout.logoutUrlPattern:#{null}}")
	private String logoutUrlPattern;

	@Value("${pac4j.logout.localLogout:#{null}}")
	private Boolean localLogout;

	@Value("${pac4j.logout.destroySession:#{null}}")
	private Boolean destroySession;

	@Value("${pac4j.logout.centralLogout:#{null}}")
	private Boolean centralLogout;

	@Autowired
	private Config config;

	@RequestMapping("/logout")
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		final J2EContext context = new J2EContext(request, response, config.getSessionStore());

		logoutLogic.perform(context, config,
				J2ENopHttpActionAdapter.INSTANCE,
				this.defaultUrl, this.logoutUrlPattern, this.localLogout, this.destroySession, this.centralLogout);
	}


}
